package com.app.server.impl;

import com.app.mapper.*;
import com.app.model.AnimalRescueModel;
import com.app.model.CategoryModel;
import com.app.server.*;
import com.app.vo.CountVo;
import com.app.vo.RescueTaskXyVo;
import com.app.vo.XyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CountNumberServiceImpl implements CountNumberService {
    //科普
    @Autowired
    private PopularScienceMapper popularScienceMapper;
    //紧急联系
    @Autowired
    private EmergentContactMapper emergentContactMapper;
    //动物记录
    @Autowired
    private AnimalRecordMapper animalRecordMapper;
    //动物救助
    @Autowired
    private AnimalRescueMapper animalRescueMapper;
    //报灾
    @Autowired
    private DisasterReportingMapper disasterReportingMapper;
    //用户
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //单位管理
    @Autowired
    private UnitsManagerMapper unitsManagerMapper;
    //单位管理 动物救助关联
    @Autowired
    private UnitsRescueService unitsRescueService;
    //单位管理 报灾救助关联
    @Autowired
    private UnitsDisasterService unitsDisasterService;
    //统计数量
    @Override
    public CountVo getCountAll() {
        int popularScienceCount = popularScienceMapper.selectCount(null);
        int emergentContactCount = emergentContactMapper.selectCount(null);
        int animalRecordCount = animalRecordMapper.selectCount(null);
        int animalRescueCount = animalRescueMapper.selectCount(null);
        int disasterReportingCount = disasterReportingMapper.selectCount(null);
        int userCount = userMapper.selectCount(null);

        CountVo countVo = new CountVo();
        countVo.setPopularScienceCount(popularScienceCount);
        countVo.setEmergentContactCount(emergentContactCount);
        countVo.setAnimalRecordCount(animalRecordCount);
        countVo.setAnimalRescueCount(animalRescueCount);
        countVo.setDisasterReportingCount(disasterReportingCount);
        countVo.setUserCount(userCount);
        return countVo;
    }
    //救援任务统计
    @Override
    public RescueTaskXyVo getRescueTaskCount() {


        RescueTaskXyVo rescueTaskVo = new RescueTaskXyVo();
        List<String> unitsNameList = unitsManagerMapper.getAllUnitsName();
        //统计已办结的动物救援任务
        List<Integer> animalData = unitsRescueService.getRescueCount();


        //统计已办结的报灾救援任务
        List<Integer> disasterCount = unitsDisasterService.getDisasterCount();
        rescueTaskVo.setXData(unitsNameList);
        rescueTaskVo.setAnimalData(animalData);
        rescueTaskVo.setDisasterData(disasterCount);

        return rescueTaskVo;
    }

    @Override
    public XyVo getAnimalProtectCount() {
        XyVo xyVo = new XyVo();
        List<String> xDate = new ArrayList<>();
        List<Integer> yDate = new ArrayList<>();
        QueryWrapper<CategoryModel> qwCategory = new QueryWrapper<>();

        qwCategory.eq("pid", 10);
        List<CategoryModel> categoryList = categoryMapper.selectList(qwCategory);
        for (CategoryModel category : categoryList) {
            QueryWrapper<AnimalRescueModel> qwRescue =new QueryWrapper<>();
            String name = category.getName();
            xDate.add(name);

            qwRescue.eq("type",category.getId());
            qwRescue.eq("allocation_status",3);
            Integer count = animalRescueMapper.selectCount(qwRescue);
            yDate.add(count);
        }
        xyVo.setXData(xDate);
        xyVo.setYData(yDate);
        return xyVo;
    }
}
